package com.railway.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.railway.bean.Contact;
import com.railway.service.UserService;
import com.railway.service.impl.UserServiceImpl;
import com.railway.util.JSONUtil;

/**
 * Servlet implementation class AddContactServlet
 */
/**用于用户中乘车人增加
 * @author 33676
 *
 */
@WebServlet("/AddContactServlet.do")
public class AddContactServlet extends HttpServlet {

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("Utf-8");
		
		int user_id = 0;
		String contact_name = "";
		String contact_phone = "";
		int contact_id_type = 0;
		String contact_id_num = "";
		int contact_passenger_type = 0;
		 
		
		
		if(request.getParameter("user_id") != null && !"".equals(request.getParameter("user_id"))) {
			user_id = Integer.parseInt(request.getParameter("user_id"));
		}
		
		if(request.getParameter("contact_name") != null && !"".equals(request.getParameter("contact_name"))) {
			contact_name = request.getParameter("user_id");
		}
		
		if(request.getParameter("contact_phone") != null && !"".equals(request.getParameter("contact_phone"))) {
			contact_phone =request.getParameter("contact_phone");
		}
		
		if(request.getParameter("contact_id_type") != null && !"".equals(request.getParameter("contact_id_type"))) {
			contact_id_type = Integer.parseInt(request.getParameter("contact_id_type"));
		}
		
		if(request.getParameter("contact_id_num") != null && !"".equals(request.getParameter("contact_id_num"))) {
			contact_id_num = request.getParameter("contact_id_num");
		}
		
		if(request.getParameter("contact_passenger_type") != null && !"".equals(request.getParameter("contact_passenger_type"))) {
			contact_passenger_type = Integer.parseInt(request.getParameter("contact_passenger_type"));
		}
		
		Contact c = new Contact(user_id, contact_name, contact_phone, contact_id_type, contact_id_num, contact_passenger_type);
		
		UserService user = new UserServiceImpl();
		
		int erroCode = user.addContact(c);
		JSONUtil.responJSOn(response, erroCode);
	}

}
